#import "CanvasView.h"


@implementation CanvasView

@synthesize easeAry;
@synthesize timer;
@synthesize timerInterval;
@synthesize touchPoints;

- (id)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
		timerInterval = 1.0 / 30.0;
		easeAry = [[NSArray alloc] initWithObjects:[NSNumber numberWithFloat:0.1645], [NSNumber numberWithFloat:0.2705], [NSNumber numberWithFloat:0.367], [NSNumber numberWithFloat:0.4545], [NSNumber numberWithFloat:0.534], [NSNumber numberWithFloat:0.605], [NSNumber numberWithFloat:0.6685], [NSNumber numberWithFloat:0.725], [NSNumber numberWithFloat:0.7745], [NSNumber numberWithFloat:0.8175], [NSNumber numberWithFloat:0.855], [NSNumber numberWithFloat:0.887], [NSNumber numberWithFloat:0.9135], [NSNumber numberWithFloat:0.9355], [NSNumber numberWithFloat:0.9535], [NSNumber numberWithFloat:0.968], [NSNumber numberWithFloat:0.979], [NSNumber numberWithFloat:0.987], [NSNumber numberWithFloat:0.99], nil];
		R = 80;
		L = [easeAry count];
		touchPoints = [[NSMutableArray alloc] init];
		touchPoints_ = [[NSMutableArray alloc] init];
		fogs = [[NSMutableArray alloc] init];
    }
    return self;
}


- (void)drawRect:(CGRect)rect
{
	CGContextRef context = UIGraphicsGetCurrentContext();
	CGContextClearRect(context, rect);
	NSMutableArray *removeFogs = [[NSMutableArray alloc] init];
	for(int i=0; i<[fogs count]; i++)
	{
		Fog *fog = [fogs objectAtIndex:i];
		int c = [fog getCount];
		CGFloat step = [[easeAry objectAtIndex:L-c] floatValue];
		CGPoint p = [fog getOverPoint:step];
		float alpha = (float)c/(float)L;
		[self drawGradientCircle:context withX:p.x withY:p.y withA:alpha withR:[fog getSize]];
		if( c <= 1 ) [removeFogs addObject:fog];
	}
	for(int i=0; i<[removeFogs count]; i++)
	{
		[fogs removeObject:[removeFogs objectAtIndex:i]];
	}
//	if( [fogs count] <= 0 ) [self stopDraw];//
}


- (void)dealloc {
    [super dealloc];
}


//タイマースタート
- (void)startDraw
{
	if( ![timer isValid] ) timer = [NSTimer scheduledTimerWithTimeInterval:timerInterval target:self selector:@selector(onTimer) userInfo:nil repeats:YES];	
}

//タイマーリセット
- (void)stopDraw
{
	[timer invalidate];
	timer = nil;
}

//タイマー実行
- (void)onTimer
{
	[self setNeedsDisplay];
}

//タッチスタート
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
	NSArray *allNewTouch = [touches allObjects];
	for(int i=0; i<[allNewTouch count]; i++)
	{
		UITouch *aTouch = [allNewTouch objectAtIndex: i];
		[touchPoints addObject:aTouch];
		[touchPoints_ addObject:[NSValue valueWithCGPoint:[aTouch locationInView:self]]];
	}
	[self startDraw];
}

//タッチ中
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
	NSEnumerator *touchEnm = [touches objectEnumerator];
	UITouch *aTouch;
	while( aTouch = [touchEnm nextObject])
	{
		int i = [touchPoints indexOfObject: aTouch];
		CGPoint from = [aTouch locationInView: self];
		CGPoint began = [[touchPoints_ objectAtIndex:i] CGPointValue];
		CGPoint to = CGPointMake( (began.x - from.x)*20.0 + (rand()%6-10) + from.x, (began.y - from.y)*20.0 + (rand()%6-10) + from.y );
		float size = R + (rand()%10-20);
		Fog *fog = [[Fog alloc] initFromPoint:from toPoint:to withSize:size withCount:L];
		[fogs addObject:fog];
		[touchPoints_ replaceObjectAtIndex:i withObject:[NSValue valueWithCGPoint:from]];
	}
}

//タッチ終了
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
}

//ベーシックな円形グラデーション描画
- (void)drawGradientCircle:(CGContextRef)context withX:(float)x withY:(float)y withA:(float)alpha withR:(float)r
{
	CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
	CGFloat components[8] = {1, 1, 1, alpha, 1, 1, 1, 0};
	size_t count = 2;//sizeof(components)/sizeof(CGFloat)*4;
	CGGradientRef gradient = CGGradientCreateWithColorComponents(colorSpace, components, nil, count);
	CGContextSetShouldAntialias( context, TRUE);
	CGContextDrawRadialGradient( context, gradient, CGPointMake(x, y), 0, CGPointMake(x, y), r, 0);
	//CGContextSetRGBFillColor( context, 1.0, 0.0, 1.0, 1.0);
	//CGContextFillEllipseInRect(context, CGRectMake(10.0, 20.0, 100.0, 200.0));
	CGGradientRelease( gradient );
	CGColorSpaceRelease( colorSpace );
}

@end
